package com.yc.cloudvideo.sp_store.controller;


import com.baomidou.mybatisplus.core.assist.ISqlRunner;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import com.yc.cloudvideo.entity.Store;
import com.yc.cloudvideo.entity.StoreUser;
import com.yc.cloudvideo.entity.User;
import com.yc.cloudvideo.sp_store.mapper.FileMapper;
import com.yc.cloudvideo.sp_store.mapper.StoreMapper;
import com.yc.cloudvideo.sp_store.mapper.StoreUserMapper;
import com.yc.cloudvideo.sp_store.mapper.UserMapper;
import com.yc.cloudvideo.util.Utils;
import com.yc.common.bean.Result;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.io.File;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LYYT
 * @since 2022-08-03
 */
@RestController
@RequestMapping("/cloudvideo/store")
public class StoreController {
    @Resource
    StoreUserMapper storeUserMapper;
    @Resource
    UserMapper userMapper;

    //
    @PostMapping("addScan")
    void addScan(long cid){
        Store store = storeMapper.selectById(cid);
        store.setScanCount(store.getScanCount()+1);
        storeMapper.updateById(store);
    }
    //申请加入仓库
    @PostMapping("joinStore")
    Result joinStore(long cid,
     long uid
    ){
        StoreUser storeuser=new StoreUser();
        storeuser.setCid(cid);
        storeuser.setUid(uid);
        storeuser.setLevel(5);
        try {
            storeUserMapper.insert(storeuser);
            return new Result(1, "提交申请成功", null);
        }catch (Exception e){
            LambdaQueryWrapper<StoreUser> lqw1=new LambdaQueryWrapper<>();
            lqw1.eq(StoreUser::getCid,cid);
            lqw1.eq(StoreUser::getUid,uid);
            lqw1.eq(StoreUser::getLevel,5);
            if(storeUserMapper.selectOne(lqw1)!=null){
                return new Result(0, "申请已经提交", null);
            }else {
                return new Result(0, "你已经加入该仓库了", null);
            }

        }
    }
    //收藏仓库
    @PostMapping("collectStore")
    Result collectStore( long cid,
                         long uid
    ) {
        StoreUser storeuser=new StoreUser();
        storeuser.setCid(cid);
        storeuser.setUid(uid);
        storeuser.setLevel(4);
        try {
            storeUserMapper.insert(storeuser);
            return new Result(1, "收藏成功", null);
        }catch (Exception e){
            LambdaQueryWrapper<StoreUser> lqw1=new LambdaQueryWrapper<>();
            lqw1.eq(StoreUser::getCid,cid);
            lqw1.eq(StoreUser::getUid,uid);
            lqw1.eq(StoreUser::getLevel,4);
            if(storeUserMapper.selectOne(lqw1)!=null){
                LambdaQueryWrapper<StoreUser> lqw2=new LambdaQueryWrapper<>();
                lqw2.eq(StoreUser::getUid,uid);
                lqw2.eq(StoreUser::getCid,cid);
                lqw2.eq(StoreUser::getLevel,4);
                storeUserMapper.delete(lqw2);
                return new Result(0, "取消收藏", null);
            }else {
                return new Result(0, "你是仓库成员，不能取消收藏", null);
            }
        }
    }
    @Resource
    StoreMapper storeMapper;
    //新建仓库
    @PostMapping("createStore")
    Result createStore(String cname,String introduce,boolean overt,long uid){
        Store store=new Store();
        store.setCname(cname);
        String path="upload\\store\\"+uid+"\\"+cname;
        store.setPath(path);
        store.setRegTime(LocalDateTime.now());
        store.setIntroduce(introduce);
        store.setOvert(overt);
        try {
            storeMapper.insert(store);
            java.io.File file = new java.io.File(Utils.toTruePath(path));
            file.mkdir();

            LambdaQueryWrapper<Store> lqw=new LambdaQueryWrapper<>();
            lqw.eq(Store::getPath,path);
            store=storeMapper.selectOne(lqw);
            StoreUser storeUser=new StoreUser();
            storeUser.setUid(uid);
            storeUser.setCid(store.getCid());
            storeUser.setLevel(1);
            storeUserMapper.insert(storeUser);
            return new Result(1,"创建成功",null);
        }catch (Exception e){
            return new Result(0,"创建失败，该仓库名已存在",null);
        }
    }

    //解散仓库
    @PostMapping("deleteStore")
    Result deleteStore(long cid){
        LambdaQueryWrapper<Store> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Store::getCid,cid);
        lqw.eq(Store::getExist,1);
        Store store = storeMapper.selectById(cid);
        store.setExist(false);
        if(storeMapper.updateById(store)==1){
            return new Result(1,"解散成功",null);
        }else{
            return new Result(0,"解散失败",null);
        }

    }
    //判断登录者是否为仓库拥有者
    @PostMapping("isCreater")
    Result isCreater(long cid,
                     long uid){
        LambdaQueryWrapper<StoreUser> lqw=new LambdaQueryWrapper<>();
        lqw.eq(StoreUser::getCid,cid);
        lqw.eq(StoreUser::getLevel,1);
        StoreUser storeUser = storeUserMapper.selectOne(lqw);
        if(storeUser.getUid().equals(uid)){
            return new Result(1,"确认解散该仓库？",null);
        }else {
            return new Result(0,"确认退出该仓库？",null);
        }

    }
    //退出仓库
    @PostMapping("quitStore")
    Result quitStore(long cid,
                     long uid) {
        LambdaQueryWrapper<StoreUser> lqw=new LambdaQueryWrapper<>();
        lqw.eq(StoreUser::getCid,cid);
        lqw.eq(StoreUser::getUid,uid);
        if(storeUserMapper.delete(lqw)==1){
            return new Result(1,"退出成功",null);
        }else{
            return new Result(0,"退出失败",null);
        }
    }
   //    搜索功能
    @PostMapping("search")
    String search(String key){
        if (key==null){
            return null;
        }
        String[]  keys=key.split(" ");

        LambdaQueryWrapper<Store> lqw=new LambdaQueryWrapper<>();
//        lqw.eq(Store::getExist,true);
//        lqw.eq(Store::getOvert,true);
//        查询stores
        for (int i = 0; i < keys.length; i++) {
            lqw.like(Store::getCname,keys[i]).or();
            if (i== keys.length-1){
                lqw.like(Store::getIntroduce,keys[i]);
            }else {
                lqw.like(Store::getIntroduce,keys[i]).or();
            }
        }
        final List<Store> stores = storeMapper.selectList(lqw);
        List<Long> cids = new ArrayList<>();
//        去掉伪删除的
        for (int i = 0; i < stores.size(); i++) {
//            if(stores.get(i).getOvert()==false){
//                stores.remove(i);
//                i--;
//                continue;
//            }
            if(stores.get(i).getExist()==false){
                stores.remove(i);
                i--;
                continue;
            }
            cids.add(stores.get(i).getCid());
        }

        List<Long> uids = new ArrayList<>();
//        查询StoreUser
        LambdaQueryWrapper<StoreUser> lqw1=new LambdaQueryWrapper<>();
        lqw1.in(StoreUser::getCid,cids);
        lqw1.eq(StoreUser::getLevel,1);
        List<StoreUser> storeUsers1 = storeUserMapper.selectList(lqw1);
        List<StoreUser> storeUsers = new ArrayList<>();
        for (int i = 0; i < cids.size(); i++) {
            for (int j = 0; j < storeUsers1.size(); j++) {
                if(storeUsers1.get(j).getCid().equals(cids.get(i))){
                    storeUsers.add(storeUsers1.get(j));
                    uids.add(storeUsers1.get(j).getUid());
                    continue;
                }
            }
        }
//       查询User
        System.out.println(uids.toString());
        List<User> users1=userMapper.selectBatchIds(uids);
        List<User> users=userMapper.selectBatchIds(uids);
        for (int i = 0; i < uids.size(); i++) {
            for (int j = 0; j < users1.size(); j++) {
                if(users1.get(j).getUid().equals(uids.get(i))){
                    users.add(users1.get(j));
                    continue;
                }
            }
        }

        HashMap<String,Object> map=new HashMap<>();
        map.put("storeUsers",storeUsers);
        map.put("stores",stores);
        map.put("creator",users);
        return new Gson().toJson(map);
    }
}

